package com.tracesystem.util;

import com.tracesystem.commons.Result;
import com.tracesystem.util.exception.GlobalException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.getInstance().setCode(500).setMsg("服务器异常");
    }
    @ExceptionHandler(GlobalException.class)
    public Result handleGlobalException(GlobalException e) {
        e.printStackTrace();
        return Result.getInstance().setCode(e.getCode()).setMsg(e.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public  Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        e.printStackTrace();
        return Result.getInstance().setCode(499).setMsg("参数异常");
    }
//    @ExceptionHandler(BindException.class)
//    public  Result handlBindException(BindException e){
//        e.printStackTrace();
//        return Result.getInstance().setCode(499).setMsg("参数异常");
//    }
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAccessDeniedException(AccessDeniedException e) {
        e.printStackTrace();
        return Result.getInstance().setCode(500).setMsg(e.getMessage());
    }
}
